#! /bin/sh
#***************************************************************************
#
# FILE: ptman
#
#       Online man page program for use with HP35635R software.
#
# Modification log:
#  01/11/90   Modified to prevent update for exact copies of files
#             ala LSIlz20472  This works so long as the list (dbman)
#             is sorted ..Ericy
#  06/11/90   Modified to look in /usr/hp3565/lib for macros ..ericy
#  07/18/90   Modified to filter out "is equal" messages ..ericy
#  11/13/90   Modified to print message if file was not found
#             this should look like "man"  (man foobar) ..ericy
#  11/14/90   Modified to truncate titles if they are too large ..ericy
#  01/24/91   Modified to handle vertical bars in tbl by use of col ..ericy
#             Fixed defect which prevented same named files from display
#  01/11/93   Modifed to search for multiple mandb's to support the
#             e14* products.  Took out the -d option and changed the -b
#             option.  For the -b option users now enter the base
#             directory of the software package .. garyw
#  07/20/93   trap on signals 1 2 and 3 to remove tmp files, otherwise
#             they get left in /tmp .. dirkh
#  01/17/97   Added /opt/e14* to list of directories. - Eric Backus
#
#***************************************************************************
#
# $Id: ptman,v 1.5 1997/02/20 20:35:02 ericb Exp $
#
REVSTR='"@(#)$Id: ptman,v 1.5 1997/02/20 20:35:02 ericb Exp $"'

BASE=""
PAGER=" | ${PAGER:-more}"
DBREVID='TiMeStAmP'
LIST=0
REVFLAG=0
OLDFILE=""
TMP_FILE="/tmp/ptman$$"
TMP_FILE2="/tmp/ptman2$$"
SECTION=""

if [ -n "${PTMAN_BASEDIR}" ]
then
   BASE="${PTMAN_BASEDIR}"
else
   if [ -s "/usr/hp3565/lib/mandb" -a -d "/usr/hp3565/man" ]
   then
      BASE="${BASE} /usr/hp3565"
   fi
   edirs=`/bin/ls -d /usr/e14* /opt/e14*`
   for dir in ${edirs}
   do
      if [ -s "${dir}/lib/mandb" -a -d "${dir}/man" ]
      then
         BASE="${BASE} ${dir}"
      fi
   done
fi

# get rid of those pesky files in case you exit
trap "rm -f ${TMP_FILE} ${TMP_FILE2}" 0 1 2 3


PROGNAME=`basename $0`

if [ -t ] ; then
	TERMIO=1
else
	TERMIO=0
fi

while [ $# -gt 0 ] ; do
	case "$1" in
	-b)
		shift
		BASE="$1"
		;;
	-l)
		LIST=1
		;;
	-p)
		shift
		PAGER=" | ${1}"
		;;
	-r)
                REVFLAG=1
		;;
	-u)
		echo "Usage: ${PROGNAME} -u"
		echo "Usage: ${PROGNAME} [-x] [-l] [-r] -b base_dir -p pager title ..."
		echo "                -b: software package base directory"
		echo "                -l: list file name only"
		echo "                -p: pager, default: more"
		echo "                -r: display revision of manual data base"
		echo "                -x: turn shell trace on"
		echo "             title: title of subject to search for"
		exit 0
		;;
	-x)
		set -x
		;;
	[1-9])  #search only the requested section 
		SECTION="${SECTION} man/${1}"
		echo found ${1} section is '${SECTION}'

		;;
	*)
		TARGET="$*"
		set -- ""
		;;
	esac
	shift
done

if [ -z "${BASE}" ]
then
   echo "Could not locate any dafault software package base directories."
   echo "You must specify a software package base directory."
   echo
   echo "Example:"
   echo "   ptman -b /usr/hp3565 ptman"
   exit 1
fi

if [ $REVFLAG -ne 0 ] ; then
    echo "Ptman source revision:   ${REVSTR}"
    echo
    for dir in ${BASE}
    do
       echo "Ptman base dir:          ${dir}/man"
       echo "Ptman database:          ${dir}/lib/mandb"
       echo "Ptman database revision: \c"
       cat ${dir}/lib/mandb | awk '$1 ~ /'${DBREVID}'/ {print $2,$3,$4,$5,$6,$7,$8,$9}'
       echo
    done
    exit 0
fi

if [ "$TARGET" = "" ] ; then
	echo "${PROGNAME}: Must specify title, -u for usage"
	exit 1
fi

for target in $TARGET ; do
    target=`echo $target | tr '[A-Z]' '[a-z]'`

    rm -f ${TMP_FILE2}

    # no specific section specified
    if [ "$SECTION" = "" ] ; then
       for dir in ${BASE} ; do
          egrep -i "^${target}" ${dir}/lib/mandb | while read line ; do
             set -- $line
             echo "${1} ${dir}/man/${2} ${3} ${dir}" >> ${TMP_FILE2}
          done
       done
    else
	# looking for a specific section(s)
	for one in ${SECTION}
	do
           for dir in ${BASE} ; do
              egrep -i "^${target}" ${dir}/lib/mandb | while read line ; do
                 set -- $line
                 echo "${1} ${dir}/man/${2} ${3} ${dir}" >> ${TMP_FILE2}
              done
           done
	done
    fi

    # cat ${TMP_FILE2}
   
    # check to see if you found a file
    if [ ! -s ${TMP_FILE2} ] ; then
	# error, zero length file
	echo "No manual entry for ${target}."
    else

	# format and display the files found
	#echo $files | while read dbline ; do
	cat ${TMP_FILE2} | while read dbline ; do
            set -- $dbline
    
    	    if [ $LIST -ne 0 ] ; then
                echo "${1} ${3} ${4}"
    	    else
    	        if [ "${OLDFILE}" != "${2}" ] ; then

		    # check to see if the strlength is greater than 20 in
		    # the first line ".TH <string>"
		    # If it is, then truncate it!  This prevents string
		    # overwriting.
		    # This only allows 6 variables
		    rm -f ${TMP_FILE}
		    awk '{ \
			    if ($1 == ".TH" ) {                         \
			      print $1 " " substr($2,1,20) " "            \
			      $3 " " $4 " " $5 " " $6 ;}                  \
			    else {                                        \
			      print $0                                    \
			    }                                             \
			 }' ${2}  > ${TMP_FILE}

		    #echo $firstline
                    if [ $TERMIO -eq 1 ] ; then
                       if [ -s "${4}/lib/macros.ptman" ] ; then
                          eval "tbl ${4}/lib/macros.ptman ${TMP_FILE} | \
			     nroff -man | col ${PAGER}"
                       else
                          eval "tbl ${TMP_FILE} | \
			     nroff -man | col ${PAGER}"
                       fi
                    else
                       if [ -s "${4}/lib/macros.ptman" ] ; then
                          eval "tbl ${4}/lib/macros.ptman ${TMP_FILE} | \
			    nroff -man | col "
                       else
                          eval "tbl ${TMP_FILE} | \
			    nroff -man | col "
                       fi
                    fi
		    rm -f ${TMP_FILE}
    	        else
    	            # duplicate entry 
    	            echo "'${2}' is equal to '${OLDFILE}' "  >> /dev/null
                fi
    	        OLDFILE=${2}
            fi
        done

    fi
    rm -f ${TMP_FILE2}

done

exit 0
